import 'package:flutter/material.dart';
import 'package:flutter_wisave/Tools/NetworkService.dart';
import 'package:flutter_wisave/widgets/AppColors.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class BreakItem extends StatelessWidget {
  final Map<String, dynamic> data;
  final double height;
  final NetworkService _networkService = NetworkService();
  final Function? onRefresh;

  BreakItem({super.key, required this.data, this.height = 150, this.onRefresh});

  get context => null;

  Future<void> _loadData(bool isAdd) async {
    try {
      final result = await _fetchData(isAdd);
      onRefresh?.call();
    } catch (e) {
      print('Error in _loadData: $e');
    }
  }

  Future<Map<String, dynamic>?> _fetchData(bool isAdd) async {
    try {
      final response = await _networkService.get(isAdd
          ? '/plcTimer/usermalfunctiontype/add?typeId=${data['typeId']}'
          : '/plcTimer/usermalfunctiontype/Delete?typeId=${data['typeId']}');
      if (response.statusCode == 200) {
        return response.data;
      } else {
        print('Failed to load data: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height, // 使用传入的高度
      padding: const EdgeInsets.all(16.0),
      margin: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0),
      decoration: BoxDecoration(
        // color: Colors.red,
        borderRadius: BorderRadius.circular(12.0), // 添加圆角
        image: DecorationImage(
          image: AssetImage(data['businessId'] == 1
              ? (AppColors.isDarkMode
                  ? 'assets/images/centre/equipment_bg_red_b.png'
                  : 'assets/images/centre/equipment_bg_red.png')
              : (AppColors.isDarkMode
                  ? 'assets/images/centre/equipment_bg_b.png'
                  : 'assets/images/centre/equipment_bg.png')),
          fit: BoxFit.cover,
        ),
      ),
      child: Container(
        // color: Colors.blue,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start, // 改为居中对齐
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            SizedBox(
              width: 42,
              height: 42,
              child: Image(
                  image:
                      AssetImage('assets/images/centre/${data['icon']!}.png')),
            ),
            Expanded(
              // 使用 Expanded 包裹 Column
              child: Column(
                mainAxisSize: MainAxisSize.min, // 设置为最小高度
                children: [
                  Text(
                    data['typeName'],
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: AppColors.textPrimary),
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  Image(
                      image: AssetImage(data['businessId'] == 1
                          ? 'assets/images/centre/equipment_btn_red.png'
                          : 'assets/images/centre/equipment_btn.png')),
                ],
              ),
            ),
            GestureDetector(
              onTap: () {
                if (data['isFavorited'] == 1) {
                  _loadData(false);
                } else {
                  _loadData(true);
                }
              },
              child: Image(
                  image: data['isFavorited'] == 1
                      ? AssetImage(AppColors.isDarkMode
                          ? 'assets/images/centre/equipment_remove_d.png'
                          : 'assets/images/centre/equipment_remove.png')
                      : AssetImage('assets/images/centre/equipment_add.png')),
            ),
          ],
        ),
      ),
    );
  }
}
